package org.example.medicalstaff.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import org.example.his.common.api.CommonResponse;
import org.example.medicalstaff.entity.*;
import org.example.medicalstaff.entity.DrugsTemplate;


import org.example.medicalstaff.mapper.DrugsDetailedMapper;
import org.example.medicalstaff.mapper.DrugsMapper;
import org.example.medicalstaff.mapper.DrugsTemplateMapper;
import org.example.medicalstaff.service.CheckService;
import org.example.medicalstaff.service.DrugService;
import org.example.medicalstaff.utils.DateStringFormat;
import org.example.medicalstaff.vo.DrugAndPreDetailed;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 周琛钧v0.1.1:URL基本都是经过修改的，只能根据数据表以及请求的格式来判断方法的写法
 */
@Service
public class DrugsServiceImpl implements DrugService {

    @Autowired
    DrugsTemplate drugsTemplate;
    @Autowired
    DrugsDetailed drugsDetailed;
    @Autowired
    DrugsTemplateMapper drugsTemplateMapper;
    @Autowired
    DrugsDetailedMapper drugsDetailedMapper;
    @Autowired
    DrugsMapper drugsMapper;

    @Override
    public CommonResponse<DrugsTemplate> medicine_manageTemplate_add(
            String Name,
            int Scope,
            int UsersID,
            String CreationTime) {
        DrugsTemplate drugsTemplate1=new DrugsTemplate();
        drugsTemplate1.setUserID(UsersID);
        drugsTemplate1.setName(Name);
        String pattern = "yyyy-MM-dd HH:mm:ss";
        Date date = DateStringFormat.parseDateTimeString(CreationTime,pattern);

        drugsTemplate1.setCreationTime(date);
        drugsTemplate1.setScope(Scope);
        int a =drugsTemplateMapper.insert(drugsTemplate1);
        if (a==1)
            return CommonResponse.createForSuccess("新增模板成功",drugsTemplate1);
        else
            return CommonResponse.createForError(1,"新增模板失败");
    }

    @Override
    public CommonResponse<String> medicine_manageTemplate_delete(
            int templateID)
    {
        QueryWrapper<DrugsDetailed> drugsDetailedQueryWrapper = new QueryWrapper<>();
        drugsDetailedQueryWrapper.eq("DrugsTempID",templateID);
        int result1 = drugsDetailedMapper.delete(drugsDetailedQueryWrapper);


        QueryWrapper<DrugsTemplate> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("ID",templateID);



        int result =drugsTemplateMapper.delete(queryWrapper);
        if(result==0){
            return CommonResponse.createForError(1,"删除模板失败");
        }else{
            return CommonResponse.createForSuccess("删除成功");
        }

    }

    @Override
    public CommonResponse<DrugsTemplate> medicine_manageTemplate_update(
            String name,//
            int scope,
            int usersID,//
            String creationTime,//
            int delMark
    ) {
        DrugsTemplate drugsTemplate2=new DrugsTemplate();
        drugsTemplate2.setScope(scope);
        drugsTemplate2.setDelMark(delMark);
        drugsTemplate2.setName(name);
        drugsTemplate2.setUserID(usersID);

        QueryWrapper<DrugsTemplate> queryWrapper=new QueryWrapper<>();
        queryWrapper.like("Name",name);
        queryWrapper.eq("UserID",usersID);
        String pattern = "yyyy-MM-dd HH:mm:ss";
        Date date = DateStringFormat.parseDateTimeString(creationTime,pattern);

        queryWrapper.eq("CreationTime",date);


        //我的想法是把要改之前的读出来保存ID,理论上应该没错
        DrugsTemplate drugsTemplate1=new DrugsTemplate();
        drugsTemplate1=drugsTemplateMapper.selectOne(queryWrapper);
        drugsTemplate2.setID(drugsTemplate1.getID());

        int result=drugsTemplateMapper.update(drugsTemplate2,queryWrapper);
        if (result==1)
            return CommonResponse.createForSuccess("修改成功",drugsTemplate2);
        else
            return CommonResponse.createForError(1,"修改失败");
    }

    @Override
    public CommonResponse<List<DrugsTemplate>> medicine_manageTemplate_getList() {
        List<DrugsTemplate>list=drugsTemplateMapper.selectList(null);
        if(list.size()>0)
            return CommonResponse.createForSuccess("获取模板列表成功",list);
        else
            return CommonResponse.createForError(1,"获取模板失败");
    }

    @Override
    public CommonResponse<DrugsTemplate> medicine_manageTemplate_get(String name) {
        QueryWrapper<DrugsTemplate> queryWrapper=new QueryWrapper<>();
        queryWrapper.like("Name",name);

        drugsTemplate=drugsTemplateMapper.selectOne(queryWrapper);
        //
        DrugsTemplate drugsTemplate3=new DrugsTemplate();
        drugsTemplate3=drugsTemplateMapper.selectOne(queryWrapper);

        if(drugsTemplate3!=null)
            return CommonResponse.createForSuccess("获取模板成功",drugsTemplate);
        else
            return CommonResponse.createForError(1,"获取模板失败");
    }

    @Override
    public CommonResponse<DrugsDetailed> medicine_manageDetailed_add(
            int DrugsID,
            int DrugsTempID,
            String DrugsUsage,
            String Dosage,
            String Frequency) {
        DrugsDetailed drugsDetailed1=new DrugsDetailed();

        drugsDetailed1.setDrugsID(DrugsID);
        drugsDetailed1.setDrugsTempID(DrugsTempID);
        drugsDetailed1.setDrugsUsage(DrugsUsage);
        drugsDetailed1.setDosage(Dosage);
        drugsDetailed1.setFrequency(Frequency);
        int result=drugsDetailedMapper.insert(drugsDetailed1);
        if (result==1)
            return CommonResponse.createForSuccess("新增模板detail成功",drugsDetailed1);
        else
            return CommonResponse.createForError(1,"新增失败");
    }

    @Override
    public CommonResponse<DrugsDetailed> medicine_manageDetailed_delete(
            String DrugsName,
            String DrugsFormat,
            int DrugsTempID) {
        QueryWrapper<Drugs> drugsQueryWrapper = new QueryWrapper<>();
        drugsQueryWrapper.eq("DrugsFormat",DrugsFormat);
        drugsQueryWrapper.eq("DrugsName",DrugsName);
        Drugs drugs = drugsMapper.selectOne(drugsQueryWrapper);
        int DrugsID = drugs.getID();

        QueryWrapper<DrugsDetailed> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("DrugsID",DrugsID);
        queryWrapper.eq("DrugsTempID",DrugsTempID);

        drugsDetailed=drugsDetailedMapper.selectOne(queryWrapper);
        int ID= drugsDetailed.getID();
        int result=drugsDetailedMapper.deleteById(ID);
        if(result==1){
            return CommonResponse.createForSuccessMessage("删除成功");
        }
        else return CommonResponse.createForError(1,"删除失败");
    }

    @Override
    public CommonResponse<List<DrugAndPreDetailed>> medicine_manageDetailed_getList1(int DrugsTempID) {
        QueryWrapper<DrugsDetailed> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("DrugsTempID",DrugsTempID);
        List<DrugAndPreDetailed> list=new ArrayList<>();
        List<DrugsDetailed> list1=new ArrayList<>();
        List<Drugs> list2=new ArrayList<>();

        list1=drugsDetailedMapper.selectList(queryWrapper);
        for(int i=0;i<list1.size();i++) {
            int DrugID = list1.get(i).getDrugsID();
            list2.add(drugsMapper.selectById(DrugID));
        }
        for(int i=0;i<list1.size();i++){
            DrugAndPreDetailed drugAndPreDetailed=new DrugAndPreDetailed();
            drugAndPreDetailed.setDrugsUsage(list1.get(i).getDrugsUsage());
            drugAndPreDetailed.setDrugsFormat(list2.get(i).getDrugsFormat());
            drugAndPreDetailed.setDrugsName(list2.get(i).getDrugsName());
            drugAndPreDetailed.setDosage(list1.get(i).getDosage());
            drugAndPreDetailed.setDrugsPrice(list2.get(i).getDrugsPrice());
            drugAndPreDetailed.setFrequency(list1.get(i).getFrequency());
            BigDecimal number=new BigDecimal(0);
            int value=12;
            number=BigDecimal.valueOf((int)value);
            drugAndPreDetailed.setAmount(number);
            list.add(drugAndPreDetailed);
        }

        if(list.size()>0)
            return CommonResponse.createForSuccess("查询成功",list);
        else
            return CommonResponse.createForError(1,"查询失败");
    }

    @Override
    public CommonResponse<DrugsDetailed> medicine_manageDetailed_update(
            int DrugsID,
            int DrugsTempID,
            String DrugsUsage,
            String Dosage,
            String Frequency) {
        DrugsDetailed drugsDetailed1=new DrugsDetailed();
        DrugsDetailed drugsDetailed2;


        drugsDetailed1.setDrugsTempID(DrugsTempID);
        drugsDetailed1.setDrugsID(DrugsID);
        drugsDetailed1.setDrugsUsage(DrugsUsage);
        drugsDetailed1.setDosage(Dosage);
        drugsDetailed1.setFrequency(Frequency);

        QueryWrapper<DrugsDetailed> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("DrugsID",DrugsID);
        queryWrapper.eq("DrugsTempID",DrugsTempID);
        drugsDetailed2=drugsDetailedMapper.selectOne(queryWrapper);
        drugsDetailed1.setID(drugsDetailed2.getID());
        int result=drugsDetailedMapper.update(drugsDetailed1,queryWrapper);

        if (result==1)
            return CommonResponse.createForSuccess("修改成功",drugsDetailed1);
        else
            return CommonResponse.createForError(1,"修改失败");
    }
    @Override
    public CommonResponse<Drugs> medicine_manageDrugs_delete(
            String DrugsName) {
        QueryWrapper<Drugs> queryWrapper=new QueryWrapper<>();

        queryWrapper.eq("DrugsName",DrugsName);

        Drugs drugs=new Drugs();
        drugs=drugsMapper.selectOne(queryWrapper);
        int ID= drugs.getID();
        int result=drugsMapper.deleteById(ID);
        if(result==1){
            return CommonResponse.createForSuccess("删除成功",drugs);
        }
        else return CommonResponse.createForError(1,"删除失败");
    }
}
